﻿using System;
using System.Collections.Generic;
using System.Linq;
using ZQNB.Common;
using ZQNB.Common.Ioc;
using Ninject;

namespace ZQNB.Web
{
    public class DependencyResolverForMvc : System.Web.Mvc.IDependencyResolver
    {
        private readonly IServiceLocator _serviceLocator = Singleton<IServiceLocator>.Instance;
        
        public object GetService(Type serviceType)
        {
            return _serviceLocator.GetInstance(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return _serviceLocator.GetAllInstances(serviceType);
        }
    }

    public class DependencyResolverForWebApi : System.Web.Http.Dependencies.IDependencyResolver
    {
        private readonly IServiceLocator _serviceLocator = Singleton<IServiceLocator>.Instance;

        public object GetService(Type serviceType)
        {
            return _serviceLocator.GetInstance(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return _serviceLocator.GetAllInstances(serviceType);
        }

        public System.Web.Http.Dependencies.IDependencyScope BeginScope()
        {
            return this;
        }

        public void Dispose()
        {
            // noop
        }
    }

    //public class DependencyResolverForSignalR : Microsoft.AspNet.SignalR.DefaultDependencyResolver
    //{

    //    private readonly IServiceLocator _serviceLocator = Singleton<IServiceLocator>.Instance;

    //    public override object GetService(Type serviceType)
    //    {

    //        return _serviceLocator.GetInstance(serviceType) ?? base.GetService(serviceType);
    //    }

    //    public override IEnumerable<object> GetServices(Type serviceType)
    //    {
    //        return _serviceLocator.GetAllInstances(serviceType).Concat(base.GetServices(serviceType));
    //    }
    //}
}